using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BAFIRCSharp;

namespace Client
{
    public partial class ChatControl : UserControl
    {
        private int _position = 0;
        private int _verticalPosition = 0;
        private int _scrollPosition = 0;
        private int _tabWidth = 0; public int TabWidth { get { return _tabWidth; } set { _tabWidth = value; } }
        private List<ChatMessage> _text = ""; public List<ChatMessage> Text { get { return _text; } set { _text = value; ComputeScrollPosition(); Invalidate(); } }

        public ChatControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            Reset();
        }

        private void Draw(Graphics g)
        {
            Color C = Color.Black;
            Font F = null;
            int Pos = 0;
            Brush B = null;
            bool NextLine = false;
            bool Last = false;
            bool HasTab = false;

            try
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                Reset();
                _scrollPosition = scrollBar.Value;

                /*while (!Last)
                {
                    
                }*/
            }
            catch
            {
            }
        }

        private void ComputeScrollPosition()
        {
            bool ScrollToBottom = false;
            if (scrollBar.Value >= scrollBar.Maximum)
                ScrollToBottom = true;

            scrollBar.Minimum = 0;
            scrollBar.Maximum = _verticalPosition - ClientRectangle.Height / 2;
            if (scrollBar.Maximum < 0)
                scrollBar.Maximum = 0;
            if (ScrollToBottom)
                scrollBar.Value = scrollBar.Maximum;
            scrollBar.SmallChange = this.Font.Height;
            scrollBar.LargeChange = scrollBar.Maximum / 10;
        }

        private void Reset()
        {
            _position = _verticalPosition = 0;
        }

        // Handlers
        private void ChatControl_Paint(object sender, PaintEventArgs e)
        {
            Draw(e.Graphics);
        }

        private void ChatControl_Resize(object sender, EventArgs e)
        {
            // resize scroll()
            ComputeScrollPosition();
            Invalidate();
        }

        private void scrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            Invalidate();
        }
    }
}
